package controllers;

import java.io.IOException;
import java.net.URLDecoder;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admin.data.GeoData;

import data.DealData;

import models.DealModel;
import models.GeoModel;

@SuppressWarnings("serial")
public class RssServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String path = ((HttpServletRequest) request).getRequestURI();
		String alias = URLDecoder.decode(
				path.toLowerCase().replaceAll("/rss/", ""), "UTF-8");

		GeoData geoData = new GeoData();
		GeoModel geo = new GeoModel();
		if (alias != null && !alias.isEmpty()) {
			geo = geoData.getGeoByAlias(alias);
		}
		if (geo == null || geo.getGeoId().equals("")) {
			geo = geoData.getFirstCity();
		}

		DealData dealData = new DealData();
		dealData.setLimit(100);
		dealData.getByCity(geo.getGeoId());
		List<DealModel> deals = dealData.getListResult();
		request.setAttribute("deals", deals);
		request.setAttribute("city", geo.getGeoId());
		try {
			request.getRequestDispatcher("/pages/rss.jsp").forward(request,
					response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
